//
//  YGPlaceholderTextView.swift
//  sinaWeiBoGui
//
//  Created by liyonggui on 16/5/21.
//  Copyright © 2016年 liyonggui. All rights reserved.
//

import UIKit

class YGPlaceholderTextView: UITextView {
    
    // MARK: - 属性
    /// 占位字符
    var placeholder: String? {
        didSet {
            placeholderLabel.text = placeholder
        }
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
   
    override init(frame: CGRect, textContainer: NSTextContainer?) {
        super.init(frame: frame, textContainer: textContainer)
//        backgroundColor = UIColor.greenColor()
        prepareUI()
        
        // 通知
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "textDidChange", name: UITextViewTextDidChangeNotification, object: self)
    }
    
    // 移除通知
    deinit {
        NSNotificationCenter.defaultCenter().removeObserver(self)
    }
    
    // 实现通知回调方法
    func textDidChange() {
        placeholderLabel.hidden = hasText()
    }
    
    private func prepareUI() {
        // 添加子控件
        addSubview(placeholderLabel)
        
        // 添加约束
        placeholderLabel.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(self).offset(5)
            make.top.equalTo(self).offset(8)
        }
    }

    // MARK: - 懒加载
    private lazy var placeholderLabel: UILabel = {
        let label = UILabel()
        
//        label.text = "请输入"
        
        label.font = UIFont.systemFontOfSize(composeTextFontSize)
        label.textColor = UIColor.lightGrayColor()
        
        return label
    }()

}
